package com.xmut.mall.order.vo;

import cn.hutool.core.text.CharSequenceUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 订单简略信息
 * 用于订单列表查看
 *
 * @author Chopper
 * @since 2020-08-17 20:28
 */
@Data
public class OrderSimpleVO {

    private Long id;

    /**
     * sn
     */
    private String sn;

    /**
     * 总价格
     */
    private Double flowPrice;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    /**
     * 创建时间
     */
    private Date createTime;


    /**
     * 订单状态
     */
    private String orderStatus;


    /**
     * 付款状态
     */
    private String payStatus;

    /**
     * 支付方式
     */
    private String paymentMethod;

    /**
     * 支付时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date paymentTime;

    /**
     * 用户名
     */
    private String memberName;

    /**
     * 店铺名称
     */
    private String storeName;

    /**
     * 店铺ID
     */
    private String storeId;


    /**
     * 订单来源
     */
    private String clientType;

    /**
     * 子订单信息
     */
    private List<OrderItemVO> orderItems;

    /**
     * item id
     */
    @Setter
    private String groupId;

    /**
     * item goods_id
     */
    @Setter
    private String groupGoodsId;

    /**
     * item sku id
     */
    @Setter
    private String groupSkuId;

    // item 数量
    @Setter
    private String groupNum;

    //    item 图片
    @Setter
    private String groupImages;

    //    item 名字
    @Setter
    private String groupName;

    //    item 编号
    @Setter
    private String groupOrderItemsSn;

    //    item 商品价格
    @Setter
    private String groupGoodsPrice;

    //    NOT_APPLIED(未申请),ALREADY_APPLIED(已申请),EXPIRED(已失效不允许申请售后)
    @Setter
    private String groupAfterSaleStatus;


    @Setter
    //    item 投诉状态
    private String groupComplainStatus;


    //    item 评价状态
    @Setter
    private String groupCommentStatus;


    //    订单类型
    private String orderType;


    //    货运状态
    private String deliverStatus;


    //    订单促销类型
    private String orderPromotionType;

    public List<OrderItemVO> getOrderItems() {
        if (CharSequenceUtil.isEmpty(groupGoodsId)) {
            return new ArrayList<>();
        }
        List<OrderItemVO> orderItemVOS = new ArrayList<>();


        String[] goodsId = groupGoodsId.split(",");

        for (int i = 0; i < goodsId.length; i++) {
            orderItemVOS.add(this.getOrderItemVO(i));
        }
        return orderItemVOS;

    }

    private OrderItemVO getOrderItemVO(int i) {
        OrderItemVO orderItemVO = new OrderItemVO();
        orderItemVO.setId(groupId.split(",")[i]);
        orderItemVO.setGoodsId(groupGoodsId.split(",")[i]);
        orderItemVO.setGoodsId(groupGoodsId.split(",")[i]);
        if (CharSequenceUtil.isNotEmpty(groupOrderItemsSn)) {
            orderItemVO.setSn(groupOrderItemsSn.split(",")[i]);
        }
        if (CharSequenceUtil.isNotEmpty(groupSkuId)) {
            orderItemVO.setSkuId(groupSkuId.split(",")[i]);
        }
        if (CharSequenceUtil.isNotEmpty(groupName)) {
            orderItemVO.setName(groupName.split(",")[i]);
        }
        if (CharSequenceUtil.isNotEmpty(groupNum) && groupNum.split(",").length == groupGoodsId.split(",").length) {
            orderItemVO.setNum(groupNum.split(",")[i]);
        }
        if (CharSequenceUtil.isNotEmpty(groupImages) && groupImages.split(",").length == groupGoodsId.split(",").length) {
            orderItemVO.setImage(groupImages.split(",")[i]);
        }
        if (CharSequenceUtil.isNotEmpty(groupAfterSaleStatus) && groupAfterSaleStatus.split(",").length == groupGoodsId.split(",").length) {
            orderItemVO.setAfterSaleStatus(groupAfterSaleStatus.split(",")[i]);
        }
        if (CharSequenceUtil.isNotEmpty(groupComplainStatus) && groupComplainStatus.split(",").length == groupGoodsId.split(",").length) {
            orderItemVO.setComplainStatus(groupComplainStatus.split(",")[i]);
        }
        if (CharSequenceUtil.isNotEmpty(groupCommentStatus) && groupCommentStatus.split(",").length == groupGoodsId.split(",").length) {
            orderItemVO.setCommentStatus(groupCommentStatus.split(",")[i]);
        }
        if (CharSequenceUtil.isNotEmpty(groupGoodsPrice) && groupGoodsPrice.split(",").length == groupGoodsId.split(",").length) {
            orderItemVO.setGoodsPrice(Double.parseDouble(groupGoodsPrice.split(",")[i]));
        }
        return orderItemVO;
    }

    /**
     * 初始化自身状态
     */
    public AllowOperation getAllowOperationVO() {
        //设置订单的可操作状态
        return new AllowOperation(this);
    }
}
